package com.sanpu.kxe.business.main.view;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseEditActivity;
import com.sanpu.kxe.business.main.bean.CertificationOldOutput;
import com.sanpu.kxe.business.main.bean.CertificationOutput;
import com.sanpu.kxe.business.main.contract.LinkfaceCheckContract;
import com.sanpu.kxe.business.main.contract.MainConfig;
import com.sanpu.kxe.business.main.presenter.LinkfaceCheckPresenter;
import com.sanpu.kxe.common.utils.CommonUtils;
import com.sanpu.kxe.common.view.CommonTitleView;
import com.sanpu.kxe.module.dialog.HintDialog;
import com.sanpu.kxe.module.immersionbar.ImmersionBar;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.module.widget.EditTextWithDel;
import com.sanpu.kxe.utils.SoftInputUtil;

import butterknife.Bind;

/**
 * Description：认证数据校验
 * Created by 薛勇军 on 2019/5/22.
 */

public class LinkfaceCheckActivity extends BaseEditActivity implements LinkfaceCheckContract.View {

    @Bind(R.id.commonTitle)
    CommonTitleView commonTitle;
    @Bind(R.id.topLl)
    LinearLayout topLl;
    @Bind(R.id.name)
    TextView vName;
    @Bind(R.id.identity_number)
    TextView vIdentityNumber;
    @Bind(R.id.bank_card_number)
    EditTextWithDel vBankcardNumber;
    @Bind(R.id.bank_card_hint)
    ImageView vBankcardHint;
    @Bind(R.id.bank_phone_number)
    EditTextWithDel vBankphoneNumber;
    @Bind(R.id.bank_phone_hint)
    ImageView vBankphoneHint;
    @Bind(R.id.next)
    RoundTextView vNext;
    @Bind(R.id.contentLl)
    LinearLayout contentLl;

    private LinkfaceCheckContract.Presenter mPresenter;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_linkface_check;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("实名认证");

        CertificationOutput certificationOutput =
                (CertificationOutput) getIntent().getSerializableExtra(MainConfig.INTENT_CERTIFICATION_INPUT);
        if (certificationOutput != null) {
            if (!TextUtils.isEmpty(certificationOutput.getIdCardName())) {
                vName.setText(certificationOutput.getIdCardName());
            }
            if (!TextUtils.isEmpty(certificationOutput.getIdCardNo())) {
                vIdentityNumber.setText(certificationOutput.getIdCardNo());
            }
        }
        mPresenter = new LinkfaceCheckPresenter(this);
        mPresenter.initDataView(certificationOutput);
    }

    @Override
    protected void setListener() {
        super.setListener();
        commonTitle.setLeftOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        vBankcardHint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView textView = new TextView(mContext);
                textView.setText("为保障账户资金安全，只能绑定认证用户本人的银行卡。");
                new HintDialog(mContext)
                        .title("结算卡号说明")
                        .content(textView)
                        .show();
            }
        });
        vBankphoneHint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView textView = new TextView(mContext);
                textView.setText("银行预留手机号码是办理该银行卡所填写的手机号码。预留手机号忘记或已停用，请联系银行客服更新处理。");
                new HintDialog(mContext)
                        .title("手机号说明")
                        .content(textView)
                        .show();
            }
        });
        vBankcardNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String current = s.toString().replace(" ", "");
                String format = CommonUtils.bankNumFormatter(current);
                vBankcardNumber.removeTextChangedListener(this);
                vBankcardNumber.setText(format);
                vBankcardNumber.addTextChangedListener(this);
            }

            @Override
            public void afterTextChanged(Editable s) {
                CharSequence text = vBankcardNumber.getText();
                if (text != null) {
                    Spannable spanText = (Spannable) text;
                    Selection.setSelection(spanText, text.length());
                }
            }
        });
        vNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isRespondEvent()) return;
                mPresenter.onCommitData(vName.getText().toString(), vIdentityNumber.getText().toString(),
                        vBankcardNumber.getText().toString(), vBankphoneNumber.getText().toString());
            }
        });
    }

    @Override
    public void showViewData(CertificationOldOutput output) {
        if (output == null) return;
        if (!TextUtils.isEmpty(output.getCrpNm())) {
            vName.setText(CommonUtils.secretIdName(output.getCrpNm()));
        }
        if (!TextUtils.isEmpty(output.getCrpIdNo())) {
            vIdentityNumber.setText(CommonUtils.secretIdNumber(output.getCrpIdNo()));
        }
        topLl.setVisibility(View.GONE);
        vBankcardHint.setVisibility(View.GONE);
        vBankphoneHint.setVisibility(View.GONE);
        contentLl.setVisibility(View.GONE);
        vName.setTextColor(ContextCompat.getColor(mContext, R.color.c_909090));
        vIdentityNumber.setTextColor(ContextCompat.getColor(mContext, R.color.c_909090));
        vNext.setVisibility(View.GONE);
    }

    @Override
    protected void onPause() {
        super.onPause();
        SoftInputUtil.hideSoftInput(this);
    }

    @Override
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarColorTransformEnable(true)
                .statusBarView(R.id.topView)
                .statusBarDarkFont(true, 0.5f)
                .fullScreen(false)
                .init();
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(LinkfaceCheckContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
